/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.util;

import java.sql.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ChiTuan
 */
public class connect {
    
    private Connection connection;
    private String id =null;
    private String pass =null;
    private String name=null;
    private String port=null;
    private String databasename=null;
    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    public int Open() {
        int ketnoi=0;
        try{
            
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        String url=String.format("jdbc:sqlserver://localhost:1433;databaseName=WebVoucher");
        connection=DriverManager.getConnection(url,"sa","123456");
        ketnoi=1;
                   
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(connect.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch(SQLException sqlexp){
           System.out.println("connect failed");
           ketnoi=0;
        }
        return ketnoi;
    }
    
    public void Close(){
        try{
            this.connection.close();
        }
        catch(SQLException ex){
            System.out.println(ex.getMessage());
        }
    }
    
    public ResultSet executeQuery(String sql){
        ResultSet rs=null;
        try{
            Statement sm=this.connection.createStatement();
            rs=sm.executeQuery(sql);
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
        }
        return rs;
    }
    
    
    
    public ResultSet getResultSet(String sql,String tensach)
    {
       ResultSet rs=null;
       try{ 
           CallableStatement statement= this.connection.prepareCall(sql);
           statement.setString(1,tensach);
           statement.execute();  
           rs=statement.getResultSet();
       }catch(Exception ex){
            System.out.println(ex.getMessage());
       }return rs;
    }
 
    public void commit(){
    try{
        this.connection.commit();
    }
    catch(SQLException ex){
        System.out.println(ex.getMessage());}
    }
}
